package client.gui;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

import packets.server.UserList;


import client.logic.Facade;
import client.logic.GuiFacade;
import encription.SecurityHandler;

/**
 * a panel that shows a list of users , and gives an option to show their details 
 */
public class pnlAllUsers extends JPanel {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblHeadline;
	private JScrollPane jScrollPane1;
	private JButton btnBack;
	private JLabel lblName;
	private JPanel pnlList;
	private JButton btnDetails;

	/**
	 * Creates an instance of pnlAllUsers
	 * @param userList the user list to view
	 */
	public pnlAllUsers(UserList userList){
		{
			this.setPreferredSize(new java.awt.Dimension(298, 154));
		}
		initGUI(userList);
	}
	
	
	/**
	 * Initializes the GUI
	 * @param userList the user list to view
	 */
	private void initGUI(final UserList userList) {
		try {
				this.setPreferredSize(new java.awt.Dimension(380, 274));
				this.setLayout(null);
				{
					jScrollPane1 = new JScrollPane();
					this.add(jScrollPane1);
					jScrollPane1.setBounds(30, 72, 300, 172);
					jScrollPane1.setAutoscrolls(true);
					{
						pnlList = new JPanel();
						jScrollPane1.setViewportView(pnlList);
						pnlList.setBounds(30, 72, 270, 172);
						pnlList.setLayout(new BoxLayout(pnlList,BoxLayout.Y_AXIS));

					}
				}
				{
					for (int i=0 ; i< userList.getLength() ; i++)
					{
						JPanel pnlUser = new JPanel();
						pnlUser.setBounds(75, 72, 191, 40);
						pnlUser.setLayout(new FlowLayout(FlowLayout.LEFT));
						pnlUser.setBorder(new LineBorder(Color.RED));
						final int tmpi = i;
						{
							btnDetails = new JButton();
							pnlUser.add(btnDetails);
							btnDetails.setText("Details");
							btnDetails.setAlignmentX(Component.LEFT_ALIGNMENT);
							btnDetails.addActionListener(new ActionListener(){
								public void actionPerformed(ActionEvent e){
									Facade.getInstance().details(userList.getIds()[tmpi]);
								}
							});
						}
						{
							lblName = new JLabel();
							pnlUser.add(lblName);
							lblName.setText(userList.getUsers(i));
							lblName.setAlignmentX(Component.LEFT_ALIGNMENT);
						}
						pnlList.add(pnlUser);
					}
				}
				lblHeadline = new JLabel();
				this.add(lblHeadline);
				lblHeadline.setText("All Users");
				lblHeadline.setBounds(98, 25, 118, 26);
				{
					btnBack = new JButton();
					this.add(btnBack);
					btnBack.setText("Back");
					btnBack.setBounds(218, 29, 72, 20);
					btnBack.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							GuiFacade.getInstance().showMainScreen();
						}
					});

				}

		} catch(Exception e) {
			SecurityHandler.catchException("initializing all users screen", e, false);
			//e.printStackTrace();
		}
	}

}
